// stdafx.cpp : source file that includes just the standard includes
//	UIlib.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "StdAfx.h"
//#include "SliderItemDelegate.h"
//#include "AbstractItemModel.h"


namespace DuiLib
{

	CSliderItemDelegate::CSliderItemDelegate()
	{
	}

	CSliderItemDelegate::~CSliderItemDelegate()
	{
	}


	CControlUI* CSliderItemDelegate::createEditor(CControlUI* target)
	{
		if (!target)
		{
			return NULL;
		}

		CSliderUI* pCtrl = new CSliderUI();
		if (pCtrl)
		{
			CContainerUI* pParent = dynamic_cast<CContainerUI*>(target->GetParent());
			if (pParent)
			{
				pParent->Add(pCtrl);

				pCtrl->SetName(_T("CSliderItemDelegateCtrl"));

				pCtrl->SetFloat(true);
				pCtrl->SetFocus();
				pCtrl->SetVisible(true);

				RECT pos = target->GetPos();
				RECT parentPos = pParent->GetPos();

				int left = pos.left - parentPos.left;
				int top = pos.top - parentPos.top;
				int width = pos.right - pos.left;
				int height = pos.bottom - pos.top;

				RECT rect = { left, top, left + width, top + height };
				pCtrl->SetFixedXY(CDuiSize(rect.left, rect.top + 10));
				pCtrl->SetFixedWidth(width);
				pCtrl->SetFixedHeight(height);

				pCtrl->SetBkImage(_T("res='common/SliderBar_BK.bmp' mask='#ffff00ff'"));
				pCtrl->SetThumbImage(_T("file='common/SliderBar_Thumb.png' mask='0xffffffff'"));
				pCtrl->SetThumbPushedImage(_T("file='common/SliderBar_Thumb.png' mask='0xffffffff'"));
				pCtrl->SetThumbHotImage(_T("file='common/SliderBar_Thumb.png' mask='0xffffffff'"));
				pCtrl->SetThumbSize(CDuiSize(12, 20));

				//pCtrl->SetBkColor(0xFFFF0000);
			}
		}

		return pCtrl;
	}

	void  CSliderItemDelegate::destroyEditor(CControlUI* editor)
	{
		if (!editor)
		{
			return;
		}

		CContainerUI* pParent = dynamic_cast<CContainerUI*>(editor->GetParent());
		if (pParent)
		{
			pParent->SetAutoDestroy(false);
			pParent->Remove(editor);
		}
	}

	void CSliderItemDelegate::setEditorData(CControlUI* editor, const CModelIndex& index)
	{
		CAbstractItemModel* pModel = model();
		CSliderUI* pSlider = dynamic_cast<CSliderUI*>(editor);
		if (!pModel || !pSlider)
		{
			return;
		}

		CVar var = pModel->data(index);
		pSlider->SetValue(var.IntVal());
	}

	void CSliderItemDelegate::setModelData(CControlUI* editor, const CModelIndex& index)
	{
		CAbstractItemModel* pModel = model();
		CSliderUI* pSlider = dynamic_cast<CSliderUI*>(editor);
		if (!pModel || !pSlider)
		{
			return;
		}

		CVar var = pSlider->GetValue();
		pModel->setData(index, var);
	}

}